<template>
  <div class="login-container">
    <el-form
      :model="loginForm"
      ref="loginForm"
      label-position="left"
      label-width="0px"
      class="card-box login-form"
    >
      <h3 class="title">系统登录</h3>
      <el-form-item prop="username">
        <span class="svg-container svg-container_login">
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-zhanghao"></use>
          </svg>
        </span>
        <el-input
          v-model="loginForm.username"
          placeholder="请输入账户"
          clearable
          style="width:65%;"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-icon_password"></use>
          </svg>
        </span>
        <el-input
          style="width:65%;"
          type="password"
          @keyup.enter.native="handleLogin"
          v-model="loginForm.password"
          placeholder="请输入密码"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item style="margin-top:-20px;">
        <span class="svg-container svg-container_login">
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-yanzhengma"></use>
          </svg>
        </span>
        <el-input
          @keyup.enter.native="handleLogin"
          v-model="loginForm.captcha"
          placeholder="验证码"
          clearable
          style="width:32%;"
          maxlength="4"
        />
        <a href="javascript:void(0)" @click="createCode()"
          ><img :src="verifitycode" style="width:30%;" alt="图片加载失败"
        /></a>
      </el-form-item>
      <el-checkbox v-model="loginForm.checked">记住密码</el-checkbox
      >&nbsp;&nbsp;
      <span style="font-size:6px;"
        >没有账号?去<a href="./register" style="color:red;">注册</a></span
      >
      <el-form-item style="margin-top:10px;">
        <el-button
          type="primary"
          style="width:70%; margin:0px 0px 0px 30px;"
          @click.native.prevent="handleLogin"
        >
          登录
        </el-button>
      </el-form-item>
      <!-- 第三方登录 -->
      <el-divider>第三方登录</el-divider>
      <div>
        <a class="icon-qq" @click="LoginClick('qq')" href="javascript:void(0);">
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-QQ"></use></svg
        ></a>
        <!-- <a
          class="icon-weixin"
          @click="LoginClick('weixin')"
          href="javascript:void(0);"
        >
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-weixin"></use></svg
        ></a> -->
        <!-- <a
          class="icon-gitee"
          @click="LoginClick('gitee')"
          href="javascript:void(0);"
        >
          <svg class="icon" aria-hidden="true">
            <use xlink:href="#icon-gitee"></use></svg
        ></a> -->
      </div>
      <p style="color: block;text-align: center;font-size: 6px;">
        Copyright ©2020-2021 辰鬼丫
      </p>
      <p style="color: block;text-align: center;">
        <em
          ><a
            href="https://beian.miit.gov.cn/"
            style="color: block; text-decoration:none;
            font-size: 6px;"
            target="_black"
            >湘ICP备2020016792号</a
          ></em
        >
      </p>
    </el-form>

    <!-- cnpm install vue-particles --save-dev
        import VueParticles from 'vue-particles'
        Vue.use(VueParticles)
    -->
    <!-- 粒子 -->
    <!-- <vue-particles
      color="#66B1FF"
      :particleOpacity="0.7"
      :particlesNumber="90"
      shapeType="circle"
      :particleSize="4"
      linesColor="#dedede"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="3"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
    > 
          //color: String类型。默认'#dedede'。粒子颜色。
          //particleOpacity: Number类型。默认0.7。粒子透明度。
          //particlesNumber: Number类型。默认80。粒子数量。
          //shapeType: String类型。默认'circle'。可用的粒子外观类型有："circle","edge","triangle", "polygon","star"。
          //particleSize: Number类型。默认80。单个粒子大小。
          //linesColor: String类型。默认'#dedede'。线条颜色。
          //linesWidth: Number类型。默认1。线条宽度。
          //lineLinked: 布尔类型。默认true。连接线是否可用。
          //lineOpacity: Number类型。默认0.4。线条透明度。
          //linesDistance: Number类型。默认150。线条距离。
          //moveSpeed: Number类型。默认3。粒子运动速度。
          //hoverEffect: 布尔类型。默认true。是否有hover特效。
          //hoverMode: String类型。默认true。可用的hover模式有: "grab", "repulse", "bubble"。
          //clickEffect: 布尔类型。默认true。是否有click特效。
      //  clickMode: String类型。默认true。可用的click模式有: "push", "remove", "repulse", "bubble"
    </vue-particles>-->
  </div>
</template>
<script>
import openWindow from '@/utils/open-window'
export default {
  name: "login",
  components: {},
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "",
        captcha: "",
        rememberMe: false
      },
      verifitycode: ""
    };
  },
  mounted() {
    this.createCode();
  },
  methods: {
    //获取验证码
    createCode() {
      //二进制方式读取图片流  验证码
      this.$axios
        .get("/getCode", {
          responseType: "arraybuffer"
        })
        .then(response => {
          return (
            "data:image/png;base64," +
            btoa(
              new Uint8Array(response.data).reduce(
                (data, byte) => data + String.fromCharCode(byte),""
              )
            )
          );
        })
        .then(data => {
          this.verifitycode = data;
        });
    },
    // 第三方登录
    LoginClick(value) {
      if (value == "qq") {
        var _this = this;
        this.$axios.get('/qq/oauth').then(resp =>{
          //window.open(resp.data.result, "_blank")
         var width = width || 900;
            var height = height || 540;
                	var left = (window.screen.width - width) / 2;
                	var top = (window.screen.height - height) / 2;
                	var win =window.open(resp.data.result, "_blank",
                		"toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, left=" +
                		left + ", top=" + top + ", width=" + width + ", height=" + height);
               //监听登录窗口是否关闭,登录成功后 后端返回关闭窗口的代码
               var listener=setInterval(function(){
                  //监听窗口是否关闭
                  if(win.closed){
                    //进入这个if代表后端验证成功!直接跳转路由
                    clearInterval(listener);//关闭监听
                    //跳转路由
                    var path = _this.$route.query.redirect;
                    _this.$router.replace({
                      path:
                        path === "/" || path === undefined ? "/admin/dashboard" : path
                    });
                    _this.$router.go(0) //刷新
                  }
                },500)

        }).catch(fail =>{
          console.error(fail)
        })
      } 
    },
    handleLogin() {
      if (!this.loginForm.username) {
        this.$message.warning("请输入账号");
        return;
      }
      if (!this.loginForm.password) {
        this.$message.warning("请输入密码");
        return;
      }
      if (!this.loginForm.captcha) {
        this.$message.warning("请输入验证码");
        return;
      }

      // eslint-disable-next-line no-redeclare
      var _this = this;
      this.$axios
        .post("/login", {
          username: this.loginForm.username,
          password: this.loginForm.password,
          rememberMe: this.loginForm.rememberMe,
          captcha: this.loginForm.captcha
        })
        .then(resp => {
          if (resp.data.code === 200) {
            var data = resp.data.result;
            _this.$store.commit("login", data);
            var path = _this.$route.query.redirect;
            _this.$router.replace({
              path:
                path === "/" || path === undefined ? "/admin/dashboard" : path
            });
          } else {
            this.$message.warning(resp.data.message);
          }
        })
        .catch(failResponse => {
          console.log(failResponse)
        });
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss" scope>
@import "../styles/mixin.scss";
$bg: #1e9fff;
$dark_gray: black;
$light_gray: black;

.card-box {
  border-radius: 10%;
  top: 5%;
  background-color: #fff;
}
.icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
  font-size: 20px;
}

.login-container {
  @include relative;
  height: 100%;
  background-color: $bg;
  input:-webkit-autofill {
    -webkit-text-fill-color: #fff !important;
  }
  input {
    background: transparent;
    border: 0px;
    -webkit-appearance: none;
    border-radius: 0px;
    padding: 12px 5px 12px 15px;
    color: $light_gray;
    height: 47px;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title {
    font-size: 26px;
    color: $light_gray;
    margin: 0px auto 30px 0px;
    text-align: center;
    font-weight: bold;
  }
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 350px;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: white;
    border-radius: 5px;
    color: white;
    width: 90%;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
  }
}
</style>
